package com.example.shoppinglist;

import fr.castorflex.android.flipimageview.library.FlipImageView;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.widget.ImageView;

public class ImageLoader {

	private int itemHeight;
	private Context context;

	public ImageLoader(Context context, int itemHeight) {
		this.context = context;
		this.itemHeight = (int) context.getResources().getDimension(itemHeight);
	}

	//setting image
	public void displayImage(ImageView imageView, int frontImageId, int backColorId, int frontColorId) {
		int backColor = context.getResources().getColor(backColorId);
		int frontColor = context.getResources().getColor(frontColorId);
		Bitmap iconFront = BitmapFactory.decodeResource(context.getResources(),frontImageId);
		BitmapDrawable drawableFront = new BitmapDrawable(context.getResources(),circleBitmap(itemHeight, iconFront, false, backColor, frontColor));
		imageView.setBackground(drawableFront); 
	}

	public void displayImage(FlipImageView imageView, int frontImageId, int backImageId, int backColorId, int frontColorId, boolean flipped) {
		//displaying image in a small circle that can flip
		int backColor = context.getResources().getColor(backColorId);
		int frontColor = context.getResources().getColor(frontColorId);
		Bitmap iconFront = BitmapFactory.decodeResource(context.getResources(),frontImageId);
		Bitmap iconBack = BitmapFactory.decodeResource(context.getResources(),backImageId);
		
		BitmapDrawable drawableFront = new BitmapDrawable(context.getResources(),circleBitmap(itemHeight, iconFront, false,  backColor, frontColor));
		BitmapDrawable drawableBack = new BitmapDrawable(context.getResources(),circleBitmap(itemHeight, iconBack, false,  backColor, frontColor));
		
		imageView.setDrawable(drawableFront);
		imageView.setFlippedDrawable(drawableBack);
		imageView.setFlipped(flipped);
	}
	
	public void setBackColor(FlipImageView imageView, int backImageId, int backColorId, int frontColorId, boolean flipped) {
		//displaying image in a small circle that can flip
		int backColor = context.getResources().getColor(backColorId);
		int frontColor = context.getResources().getColor(frontColorId);
		Bitmap iconBack = BitmapFactory.decodeResource(context.getResources(),backImageId);
		
		BitmapDrawable drawableBack = new BitmapDrawable(context.getResources(),circleBitmap(itemHeight, iconBack, false,  backColor, frontColor));
		
		imageView.setFlippedDrawable(drawableBack);
		imageView.setFlipped(flipped);
	}
	
	public void setFrontColor(FlipImageView imageView, int frontImageId, int backColorId, int frontColorId, boolean flipped) {
		//displaying image in a small circle that can flip
		int backColor = context.getResources().getColor(backColorId);
		int frontColor = context.getResources().getColor(frontColorId);
		Bitmap iconFront = BitmapFactory.decodeResource(context.getResources(),frontImageId);
		
		BitmapDrawable drawableFront = new BitmapDrawable(context.getResources(),circleBitmap(itemHeight, iconFront, false,  backColor, frontColor));

		imageView.setDrawable(drawableFront);
		imageView.setFlipped(flipped);
	}
	

	public static Bitmap circleBitmap(int height, Bitmap bitmap, boolean scale, int backColor, int frontColor) {
		//landscape and portrait cropping
		int bitmapWidth = bitmap.getWidth();
		int bitmapHeight = bitmap.getHeight();
		Bitmap croppedBitmap;
		Bitmap scaled;
		if(scale){
			if( bitmapWidth > bitmapHeight){
				//find centre of image horizontally
				int centre = bitmapWidth/2;
				//divide height in half, as height is what we want to match to the width
				int halfNewWidth = bitmapHeight/2;
				//find where to measure from on the left side of the image
				//so a square image is cropped from the centre of the old one
				int left = centre - halfNewWidth;
				croppedBitmap = Bitmap.createBitmap(bitmap, left, 0, bitmapHeight, bitmapHeight);
			}else{
				//find centre of image vertically
				int centre = bitmapHeight/2;
				//divide width in half, as width is what we want to match to the height
				int halfNewHeight = bitmapWidth/2;
				//find where to measure from on the bottom side of the image
				//so a square image is cropped from the centre of the old one
				int bottom = centre - halfNewHeight;
				croppedBitmap = Bitmap.createBitmap(bitmap, 0, bottom, bitmapWidth, bitmapWidth);
			}
			scaled = Bitmap.createScaledBitmap(croppedBitmap, height, height, false);
		}else{
			int addToSides = (height+10-bitmapHeight)/2;
			scaled = Bitmap.createBitmap(height+10, height+10, Bitmap.Config.ARGB_8888);
			Canvas can = new Canvas(scaled);
			can.drawBitmap(bitmap, addToSides, addToSides, null);
		}
		
		Bitmap circleBitmap = Bitmap.createBitmap(height+10, height+10, Bitmap.Config.ARGB_8888);
		//making circles behind image to give it a coloured ring around the image
		//also so when the item in list is interfered with this can change the image to the tick icon
		BitmapShader shader = new BitmapShader (scaled,  TileMode.CLAMP, TileMode.CLAMP);
		Paint paint1 = new Paint();
		paint1.setAntiAlias(true);
		paint1.setShader(shader);
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		paint.setColor(backColor);
		paint.setStrokeWidth(5);
		paint.setStyle(Paint.Style.STROKE);
		Paint paint2 = new Paint();
		paint2.setAntiAlias(true);
		paint2.setColor(frontColor);
		paint2.setStyle(Paint.Style.FILL);

		Canvas canvas = new Canvas(circleBitmap);
		canvas.drawCircle((height+10)/2, (height+10)/2, (height)/2, paint);
		canvas.drawCircle((height+10)/2, (height+10)/2, height/2, paint2);
		canvas.drawCircle((height+10)/2, (height+10)/2, height/2, paint1);
		return circleBitmap;
	}
}
